/*******************************************************************************
** 文件名称：page_setup.c
** 文件作用：设置页面
** 编写作者：Tom Free 付瑞彪
** 编写时间：2021-05-30
** 文件备注：
** 更新记录：
**           2021-05-30 -> 创建文件
**                                                            <Tom Free 付瑞彪>
**
**           Copyright (c) 2018-2021 付瑞彪 All Rights Reserved
**
**       1 Tab == 4 Spaces     UTF-8     ANSI C Language(C99)
*******************************************************************************/

#include "app_inc.h"

/* 页面本地参数结构 */
typedef struct
{
    gm_time_t time;     /* 当前操作的时间 */
    uint8_t   nop_time; /* 未操作时间，用于自动退出 */
    bool      blink;    /* 用于闪烁状态 */
    uint8_t   cnt;      /* 内部计数器 */
    uint8_t   opt_val;  /* 当前操作值 */
    uint8_t   max_val;  /* 操作最大值 */
    bool      modify;   /* 是否有修改过数据 */
    bool      repeat_en;/* 是否使能重复长按动作 */
} page_local_args_t;

/* 本地参数 */
static page_local_args_t local_args;

/* 进入页面回调 */
static void page_time_setup_into_callback(page_id_t id)
{
    if (id == PAGE_SET_TIME_HOUR)
    {
        /* 进入时设置时读取时间 */
        ds3231_read_time(&local_args.time);
        local_args.opt_val = local_args.time.hour;
        local_args.max_val = 11;
        local_args.modify = false;
        led_bg_enable();
    }
    else if (id == PAGE_SET_TIME_MINUTE)
    {
        local_args.opt_val = local_args.time.minute;
        local_args.max_val = 59;
    }
    else if (id == PAGE_SET_TIME_SECOND)
    {
        local_args.opt_val = local_args.time.second;
        local_args.max_val = 59;
    }
    /* 设置参数 */
    local_args.repeat_en = false;
    local_args.nop_time = 0;
    local_args.blink = true;
    /* 显示时间 */
    led_write_time(&local_args.time);
    local_args.cnt = 0;
}

/* 事件处理回调 */
static void page_time_setup_exec_callback(page_id_t id, page_event_t evt)
{
    bool flag = false;
    switch (evt)
    {
    case PAGE_EVENT_TIMER_100MS:
        if (++local_args.cnt < 3)
        {
            break;
        }
        local_args.cnt = 0;
        if (local_args.blink)
        {
            local_args.blink = false;
            /* 不显示 */
            if (id == PAGE_SET_TIME_HOUR)
            {
                led_clear_hour();
            }
            else if (id == PAGE_SET_TIME_MINUTE)
            {
                led_clear_minute();
            }
            else if (id == PAGE_SET_TIME_SECOND)
            {
                led_clear_second();
            }
        }
        else
        {
            local_args.blink = true;
            /* 显示 */
            led_write_time(&local_args.time);
        }
        local_args.nop_time++;
        if (local_args.nop_time >= 60)
        {
            /* 切回时间显示 */
            page_mgr_switch(PAGE_TIME_DISPLAY);
            bell_play(10, 2);
        }
        break;

    case PAGE_EVENT_BTN_MODE_LONG_PRESS:
        local_args.repeat_en = true;
        if (id == PAGE_SET_TIME_HOUR)
        {
            /* 切回到设置分 */
            page_mgr_switch(PAGE_SET_TIME_MINUTE);
            bell_play(50, 1);
        }
        else if (id == PAGE_SET_TIME_MINUTE)
        {
            /* 切回到设置秒 */
            page_mgr_switch(PAGE_SET_TIME_SECOND);
            bell_play(50, 1);
        }
        else if (id == PAGE_SET_TIME_SECOND)
        {
            if (local_args.modify)
            {
                /* 设置时间 */
                ds3231_write_time(&local_args.time);
                bell_play(50, 1);
            }
            else
            {
                bell_play(10, 2);
            }
            /* 切回时间显示 */
            page_mgr_switch(PAGE_TIME_DISPLAY);
        }
        break;

    case PAGE_EVENT_BTN_INC_LONG_PRESS:
        local_args.repeat_en = true;
    case PAGE_EVENT_BTN_INC_CLICK:
        bell_play(5, 1);
        flag = true;
    case PAGE_EVENT_BTN_INC_LONG_REPEAT:
        if ((!flag) && (!local_args.repeat_en))
        {
            break;
        }
        if (local_args.opt_val >= local_args.max_val)
        {
            local_args.opt_val = 0;
        }
        else
        {
            local_args.opt_val++;
        }
        local_args.modify = true;
        local_args.nop_time = 0;
        local_args.blink = true;
        local_args.cnt = 0;
        /* 显示时间 */
        if (id == PAGE_SET_TIME_HOUR)
        {
            local_args.time.hour = local_args.opt_val;
        }
        else if (id == PAGE_SET_TIME_MINUTE)
        {
            local_args.time.minute = local_args.opt_val;
        }
        else if (id == PAGE_SET_TIME_SECOND)
        {
            local_args.time.second = local_args.opt_val;
        }
        led_write_time(&local_args.time);
        break;

    case PAGE_EVENT_BTN_DEC_LONG_PRESS:
        local_args.repeat_en = true;
    case PAGE_EVENT_BTN_DEC_CLICK:
        bell_play(5, 1);
        flag = true;
    case PAGE_EVENT_BTN_DEC_LONG_REPEAT:
        if ((!flag) && (!local_args.repeat_en))
        {
            break;
        }
        if (local_args.opt_val == 0)
        {
            local_args.opt_val = local_args.max_val;
        }
        else
        {
            local_args.opt_val--;
        }
        local_args.modify = true;
        local_args.nop_time = 0;
        local_args.blink = true;
        local_args.cnt = 0;
        /* 显示时间 */
        if (id == PAGE_SET_TIME_HOUR)
        {
            local_args.time.hour = local_args.opt_val;
        }
        else if (id == PAGE_SET_TIME_MINUTE)
        {
            local_args.time.minute = local_args.opt_val;
        }
        else if (id == PAGE_SET_TIME_SECOND)
        {
            local_args.time.second = local_args.opt_val;
        }
        led_write_time(&local_args.time);
        break;

    case PAGE_EVENT_BTN_MODE_CLICK:
    case PAGE_EVENT_BTN_MODE_LONG_REPEAT:
        local_args.nop_time = 0;
        break;

    default:
        break;
    }
}

/* 导出页面 */
PAGE_EXPORT(time_setup,
            .into_cb = page_time_setup_into_callback,
            .exec_cb = page_time_setup_exec_callback,
            .poll_cb = NULL,
            .exit_cb = NULL);

/* 进入页面回调 */
static void page_alarm_setup_into_callback(page_id_t id)
{
    if (id == PAGE_SET_ALARM_HOUR)
    {
        /* 进入闹钟时读取时间 */
        local_args.time.hour = eeprom_variable.alarm_time.hour;
        local_args.time.minute = eeprom_variable.alarm_time.minute;
        local_args.time.second = eeprom_variable.alarm_time.second;
        local_args.opt_val = local_args.time.hour;
        local_args.max_val = 11;
        local_args.modify = false;
        led_bg_enable();
    }
    else if (id == PAGE_SET_ALARM_MINUTE)
    {
        local_args.opt_val = local_args.time.minute;
        local_args.max_val = 59;
    }
    else if (id == PAGE_SET_ALARM_SECOND)
    {
        local_args.opt_val = local_args.time.second;
        local_args.max_val = 59;
    }
    /* 设置参数 */
    local_args.repeat_en = false;
    local_args.nop_time = 0;
    local_args.blink = true;
    /* 显示时间 */
    led_write_time(&local_args.time);
    local_args.cnt = 0;
}

/* 事件处理回调 */
static void page_alarm_setup_exec_callback(page_id_t id, page_event_t evt)
{
    bool flag = false;

    switch (evt)
    {
    case PAGE_EVENT_TIMER_100MS:
        if (++local_args.cnt < 3)
        {
            break;
        }
        local_args.cnt = 0;
        if (local_args.blink)
        {
            local_args.blink = false;
            /* 不显示 */
            if (id == PAGE_SET_ALARM_HOUR)
            {
                led_clear_hour();
            }
            else if (id == PAGE_SET_ALARM_MINUTE)
            {
                led_clear_minute();
            }
            else if (id == PAGE_SET_ALARM_SECOND)
            {
                led_clear_second();
            }
        }
        else
        {
            local_args.blink = true;
            /* 显示 */
            led_write_time(&local_args.time);
        }
        local_args.nop_time++;
        if (local_args.nop_time >= 60)
        {
            /* 切回时间显示 */
            page_mgr_switch(PAGE_TIME_DISPLAY);
            bell_play(10, 2);
        }
        break;

    case PAGE_EVENT_BTN_MODE_LONG_PRESS:
        local_args.repeat_en = true;
        if (id == PAGE_SET_ALARM_HOUR)
        {
            /* 切回到设置分 */
            page_mgr_switch(PAGE_SET_ALARM_MINUTE);
            bell_play(50, 1);
        }
        else if (id == PAGE_SET_ALARM_MINUTE)
        {
            /* 切回到设置秒 */
            page_mgr_switch(PAGE_SET_ALARM_SECOND);
            bell_play(50, 1);
        }
        else if (id == PAGE_SET_ALARM_SECOND)
        {
            /* 设置时间 */
            eeprom_variable_set_alarm_time(&local_args.time);
            eeprom_variable_set_alarm_status(true);
            bell_play(50, 1);
            /* 切回到时间显示页面 */
            page_mgr_switch(PAGE_TIME_DISPLAY);
        }
        break;

    case PAGE_EVENT_BTN_INC_LONG_PRESS:
        local_args.repeat_en = true;
    case PAGE_EVENT_BTN_INC_CLICK:
        bell_play(5, 1);
        flag = true;
    case PAGE_EVENT_BTN_INC_LONG_REPEAT:
        if ((!flag) && (!local_args.repeat_en))
        {
            break;
        }
        if (local_args.opt_val >= local_args.max_val)
        {
            local_args.opt_val = 0;
        }
        else
        {
            local_args.opt_val++;
        }
        local_args.modify = true;
        local_args.nop_time = 0;
        local_args.blink = true;
        local_args.cnt = 0;
        /* 显示时间 */
        if (id == PAGE_SET_ALARM_HOUR)
        {
            local_args.time.hour = local_args.opt_val;
        }
        else if (id == PAGE_SET_ALARM_MINUTE)
        {
            local_args.time.minute = local_args.opt_val;
        }
        else if (id == PAGE_SET_ALARM_SECOND)
        {
            local_args.time.second = local_args.opt_val;
        }
        led_write_time(&local_args.time);
        break;

    case PAGE_EVENT_BTN_DEC_LONG_PRESS:
        local_args.repeat_en = true;
    case PAGE_EVENT_BTN_DEC_CLICK:
        bell_play(5, 1);
        flag = true;
    case PAGE_EVENT_BTN_DEC_LONG_REPEAT:
        if ((!flag) && (!local_args.repeat_en))
        {
            break;
        }
        if (local_args.opt_val == 0)
        {
            local_args.opt_val = local_args.max_val;
        }
        else
        {
            local_args.opt_val--;
        }
        local_args.modify = true;
        local_args.nop_time = 0;
        local_args.blink = true;
        local_args.cnt = 0;
        /* 显示时间 */
        if (id == PAGE_SET_ALARM_HOUR)
        {
            local_args.time.hour = local_args.opt_val;
        }
        else if (id == PAGE_SET_ALARM_MINUTE)
        {
            local_args.time.minute = local_args.opt_val;
        }
        else if (id == PAGE_SET_ALARM_SECOND)
        {
            local_args.time.second = local_args.opt_val;
        }
        led_write_time(&local_args.time);
        break;

    case PAGE_EVENT_BTN_MODE_CLICK:
    case PAGE_EVENT_BTN_MODE_LONG_REPEAT:
        local_args.nop_time = 0;
        break;

    default:
        break;
    }
}

/* 导出页面 */
PAGE_EXPORT(alarm_setup,
            .into_cb = page_alarm_setup_into_callback,
            .exec_cb = page_alarm_setup_exec_callback,
            .poll_cb = NULL,
            .exit_cb = NULL);
